﻿#include "systemsetting.h"
#include "ui_systemsetting.h"

SystemSetting* SystemSetting::m_instance = nullptr;
QMutex SystemSetting::m_instanceMutex;

SystemSetting* SystemSetting::instance(QWidget *parent)
{
    QMutexLocker locker(&m_instanceMutex);
    if (!m_instance) {
        m_instance = new SystemSetting(parent);
    }
    return m_instance;
}

SystemSetting::SystemSetting(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::SystemSetting)
{
    ui->setupUi(this);
    slots_serialRefresh();
    InitSystemSet();
}

void SystemSetting::InitSystemSet()
{
    QSettings set_config(LOG_PATH+"/config.ini",QSettings::IniFormat);
    m_serialName = set_config.value("serial/serial_name").toString();
    m_serialBaud = set_config.value("serial/serial_baud").toInt();
    m_timeout = set_config.value("timeout").toInt();
    m_autoMode = set_config.value("automode").toBool();
    ui->cbx_serialName->setCurrentText(m_serialName);
    ui->cbx_serialBaud->setCurrentText(QString::number(m_serialBaud));
    ui->cbx_autoMode->setChecked(m_autoMode);
    ui->edt_timeOut->setText(QString::number(m_timeout));
}

SystemSetting::~SystemSetting()
{
    delete ui;
}

QString SystemSetting::serialName() const
{
    QMutexLocker locker(&m_mutex);
    return m_serialName;
}

QSerialPort::BaudRate SystemSetting::serialBaud() const
{
    QMutexLocker locker(&m_mutex);
    return (QSerialPort::BaudRate)m_serialBaud;
}

qint32 SystemSetting::timeout() const
{
    QMutexLocker locker(&m_mutex);
    return m_timeout;
}

bool SystemSetting::autoMode() const
{
    QMutexLocker locker(&m_mutex);
    return m_autoMode;
}

void SystemSetting::setSerialOpened(const bool isopen)
{
    QMutexLocker locker(&m_mutex);
    this->m_serialOpened = isopen;
}

void SystemSetting::on_btn_saveSet_clicked()
{
    QSettings set_config(LOG_PATH+"/config.ini",QSettings::IniFormat);
    QString serial_name = ui->cbx_serialName->currentText();
    QSerialPort::BaudRate baud_rate = static_cast<QSerialPort::BaudRate>(ui->cbx_serialBaud->currentText().toUInt());
    qint32 newTimeout = ui->edt_timeOut->text().toUInt();
    bool newAutoMode = ui->cbx_autoMode->isChecked();
    // 使用互斥锁保护成员变量
    {
        QMutexLocker locker(&m_mutex);
        if ( !m_serialOpened || (serial_name != m_serialName || baud_rate != m_serialBaud) ) {
            m_serialName = serial_name;
            m_serialBaud = baud_rate;
            set_config.setValue("serial/serial_name",m_serialName);
            set_config.setValue("serial/serial_baud",m_serialBaud);
            emit sig_changeSerial(m_serialName, m_serialBaud);
        }
        if (newTimeout != m_timeout) {
            m_timeout = newTimeout;
            set_config.setValue("timeout",newTimeout);
            emit sig_changeTimeout(m_timeout);
        }
        if (newAutoMode != m_autoMode) {
            m_autoMode = newAutoMode;
            set_config.setValue("automode",m_autoMode);
            emit sig_changeAutoMode(m_autoMode);
         }
    }
}

void SystemSetting::slots_serialRefresh()
{
    ui->cbx_serialName->clear();
    m_serialList.clear();
    foreach ( const QSerialPortInfo &info, QSerialPortInfo::availablePorts() ) {
        qDebug() << __FUNCTION__
                 << "scan serial name:" << info.portName()
                 << "serial description:" << info.description();
        m_serialList.append(info.portName());
    }
    ui->cbx_serialName->addItems(m_serialList);
}

